<?php

namespace App\Http\Controllers\Mobile;

use App\Interfaces\CategoryService;
use App\Interfaces\GoodsService;
use App\Interfaces\SolrService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Category;
class SearchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,SolrService $solrService)
    {
        $keyWords = $solrService->suggest('',7);
        $shop_id = $request->shop_id;
        //dd($keyWords);
        return view('mobile.search.index',compact('keyWords','shop_id'));
    }

    public function result(Request $request,SolrService $solrService,GoodsService $goodsService){
        $filter['keyword'] = addslashes($request->get('keyword'));
        $filter['cat_id'] = $request->get('cat_id');
        $filter['brand_id'] = $request->get('brand_id')?$request->get('brand_id'):0;
        $filter['stock'] = $request->get('stock')?$request->get('stock'):0;
        $filter['sort'] = $request->get('sort');
        $filter['shop_id'] = $request->get('shop_id');
        $result = $solrService->search($filter['keyword'],$filter['cat_id'],$filter['brand_id'],$filter['stock'],$request->get('page'),6,$filter['sort'],$filter['shop_id']);
//dd($result);
        //$goodsPaginate = $result['paginate'];

        $goodsArr = [];
        foreach($result['paginate']->items() as $document ){
            $goodsArr[] = $goodsService->get($document->getFields()['goods_id']);
        }
        $page = $request->get('page') ?: Paginator::resolveCurrentPage('page');
        $paginate =  new LengthAwarePaginator($goodsArr, $result['paginate']->total(), 40, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page',
        ]);
        $type = 'keywords_search';
        return view('mobile.search.result',compact('paginate','filter','type'));
    }

    public function getResult(Request $request,SolrService $solrService,GoodsService $goodsService,CategoryService $categoryService){
        //dd($request->all());
        $keyword =addslashes($request->get('keyword'));
        $page = $request->get('page');
        $type = $request->get('type');
        //dd($type);
        $goods = [];
        if($type == "keywords_search"){
            $filter['cat_id'] = $request->get('cat_id');
            $filter['brand_id'] = $request->get('brand_id')?$request->get('brand_id'):0;
            $filter['stock'] = $request->get('stock')?$request->get('stock'):0;
            $filter['sort'] = $request->get('sort');

            $result = $solrService->search($keyword,$filter['cat_id'],$filter['brand_id'],$filter['stock'],$page,6,$filter['sort']);

            $goodsArr = [];
            foreach($result['paginate']->items() as $document ){
                $goodsArr[] = $goodsService->get($document->getFields()['goods_id']);
            }
            $page = $request->get('page') ?: Paginator::resolveCurrentPage('page');
            $paginate =  new LengthAwarePaginator($goodsArr, $result['paginate']->total(), 40, $page, [
                'path' => Paginator::resolveCurrentPath(),
                'pageName' => 'page',
            ]);//dd($paginate);
            $goods = $paginate->toArray();
        }else if($type == "cat_search") {
            $filter['cat_id'] = $request->get('cat_id');
            //dd($filter);
            $cat = Category::find($filter['cat_id']);
            if (!$cat) abort(404);
            $depth = $cat->getDepth();
            if ($depth == 2 || $depth == 3) {
                if ($depth == 2) {
                    $cat_ids = $categoryService->getChildIds($filter['cat_id'])->toArray();
                } else {
                    $cat_ids = $categoryService->getBrotherIds($filter['cat_id'])->toArray();
                }

                if ($depth == 3 && !$request->get('cat_id')) {
                    $filter['cat_id'] = $filter['cat_id'];
                    //检索分类
                    $filterCategory = $categoryService->getPath($filter['cat_id']);
                } elseif ($depth == 3 && $request->get('cat_id')) {
                    $filter['cat_id'] = $request->get('cat_id');
                    //检索分类
                    $filterCategory = $categoryService->getPath($request->get('cat_id'));
                } elseif ($depth != 3 && $request->get('cat_id')) {
                    $filter['cat_id'] = $request->get('cat_id');
                    //检索分类
                    $filterCategory = $categoryService->getPath($request->get('cat_id'));
                } elseif ($depth != 3 && !$request->get('cat_id')) {
                    $filter['cat_id'] = $request->get('cat_id');
                    //检索分类
                    $filterCategory = $categoryService->getPath($filter['cat_id']);
                }
                $filter['keyword'] = addslashes($request->get('keyword'));
                $filter['cat_id'] = $request->get('cat_id');
                $filter['brand_id'] = $request->get('brand_id') ? $request->get('brand_id') : 0;
                $filter['stock'] = $request->get('stock') ? $request->get('stock') : 0;
                $filter['sort'] = $request->get('sort');

                $result = $solrService->category($cat_ids, $filter['cat_id'], $filter['brand_id'], $filter['stock'], $request->get('page'), 6, $filter['sort']);

                $goodsArr = [];
                foreach ($result['paginate']->items() as $document) {
                    $goodsArr[] = $goodsService->get($document->getFields()['goods_id']);
                }
                $page = $request->get('page') ?: Paginator::resolveCurrentPage('page');
                $paginate = new LengthAwarePaginator($goodsArr, $result['paginate']->total(), 40, $page, [
                    'path' => Paginator::resolveCurrentPath(),
                    'pageName' => 'page',
                ]);
                $goods = $paginate->toArray();
            }
        }
        //dd($goods);
        return Response()->json(['goods'=>$goods,'status'=>'1']);

    }

}
